package com.ysb.util;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collection;

public class StringUtil {

	
	public static String encodeUrl(String url, String charset) {
		String encodedUrl="";
		try {
			encodedUrl=URLEncoder.encode(url,charset).replace("+", "%20");
		} catch(Throwable t) {
			
		}
		return encodedUrl;
	}
	
	public static String decodeUrl(String url) {
		String decodedUrl="";
		try {
			decodedUrl=URLDecoder.decode(url,"utf-8");
		} catch(Throwable t) {
			
		}
		return decodedUrl;
	}
	
	public static String decodeUrl(String url, String charset) {
		String decodedUrl="";
		try {
			decodedUrl=URLDecoder.decode(url,charset);
		} catch(Throwable t) {
			
		}
		return decodedUrl;
	}
	
	
	public static String join(Object[] objs, String seperator) {
		StringBuilder sb = new StringBuilder();
		for(Object obj:objs) {
			sb.append(obj);
			sb.append(seperator);
		}
		return sb.toString();
	}
	
	public static String join(Collection<?> objs, String seperator) {
		StringBuilder sb = new StringBuilder();
		int count = objs.size();
		for(Object obj:objs) {
			sb.append(obj);
			--count;
			if(count>0) {
				sb.append(seperator);
			}
		}
		return sb.toString();
	}
	
	public static String join(Object[] objs) {
		return join(objs, "|");
	}
	
	public static String join(Object obj, int count, String seperator) {
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<count;i++) {
			sb.append(String.valueOf(obj));
			if(i<count-1) {
				sb.append(seperator);
			}
		}
		return sb.toString();
	}
	
	public static boolean isEmpty(String str) {
		return str == null || str.isEmpty() || str.trim().length()==0;
	}
	
	public static int convertInt(String str, int def) {
		try {
			if(str == null) return def;
			return Integer.valueOf(str);
		}
		catch(NumberFormatException th) {
			return def;
		}
	}
	
	public static String encodeSQL(String sql) {
		if (sql == null)
		{
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < sql.length(); ++i)
		{
			char c = sql.charAt(i);
			switch(c)
			{
			case '\\':
				sb.append("\\\\");
				break;
			case '\r':
				sb.append("\\r");
				break;
			case '\n':
				sb.append("\\n");
				break;
			case '\t':
				sb.append("\\t");
				break;
			case '\b':
				sb.append("\\b");
				break;
			case '\'':
				sb.append("\'\'");
				break;
			case '\"':
				sb.append("\\\"");
				break;
			case '\u200B':	
			case '\uFEFF':
				break;
			default:
				sb.append(c);
			}
		}
		return sb.toString();
	}
	
	public static boolean convertBoolean(String str, boolean def) {
		try {
			if(str == null) return def;
			return Boolean.valueOf(str);
		}
		catch(Throwable th) {
			return def;
		}
	}
	
	public static double convertDouble(String str, double def) {
		try {
			if(str == null) return def;
			return Double.valueOf(str);
		}
		catch(NumberFormatException th) {
			return def;
		}
	}
	
	public static long converLong(String str, long def) {
		try {
			if(str == null) return def;
			return Long.valueOf(str);
		}
		catch(NumberFormatException th) {
			return def;
		}
	}
}
